import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login'
import home from '../components/home'
import welcome from '../components/welcome'
import user from '../components/user'
import rights from '../components/rights'
import roles from '../components/rights/roles'
import categories from '../components/goods/categories'
import params from '../components/params'
import List from '../components/List'
import addGoods from '../components/goods/addGoods'
import order from '../components/order'
import report from '../components/report'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  { 
    path: '/home',
    redirect: '/welcome',
    component: home,
    children: [
      {
        path: '/welcome',
        component: welcome
      },
      {
        path: '/users',
        component: user
      },
      {
        path: '/rights',
        component: rights
      },
      {
        path: '/roles',
        component: roles
      },
      {
        path: '/categories',
        component: categories
      }, 
      {
        path: '/params',
        component: params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/addPage',
        component: addGoods
      },
      {
        path: '/orders',
        component: order
      },
      {
        path: '/reports',
        component: report
      }
    ]
  
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from , next) => {
  if(to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
